/*
 * Copyright 2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.pajamas.starter.integration.module.jpa;

import org.pajamas.starter.integration.module.context.PajamasPropSrcConstants;
import org.pajamas.starter.integration.module.embedded.PajamasEmbeddedAutoConfiguration;
import org.pajamas.starter.integration.spring.bean.BeanConstants;
import org.pajamas.starter.integration.spring.jdbc.HibernatePropertiesEnum;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.annotation.Order;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import javax.sql.DataSource;

/**
 * @author william
 * @since 2023/12/28
 */
@ConditionalOnProperty(prefix = "org.pajamas.starter.jpa", name = "enabled", havingValue = "true", matchIfMissing =
        true)
@ConditionalOnBean(DataSource.class)
@AutoConfigureAfter({DataSourceAutoConfiguration.class, PajamasEmbeddedAutoConfiguration.class})
@EnableJpaRepositories(basePackages = "${" + PajamasPropSrcConstants.BASE_PACKAGE_REPO + "}",
        repositoryFactoryBeanClass = PajamasRepoFactoryBean.class)
@EnableConfigurationProperties(PajamasJpaProperties.class)
@EnableJpaAuditing
public class PajamasJpaAutoConfiguration {
    @ConditionalOnMissingBean(AuditorAware.class)
    @Bean
    public AuditorAware<Long> auditorAware(PajamasJpaProperties properties) {
        return new PajamasUserAuditor(properties.getDefaultUserId());
    }

    /**
     * @see org.springframework.boot.autoconfigure.orm.jpa
     * <p>
     * Priority higher than configuration-files
     */
    @ConditionalOnProperty(prefix = "org.pajamas.starter.jpa", name = "hibernate", havingValue = "true",
            matchIfMissing = true)
    @Order(BeanConstants.ORDER_LATEST_SECOND)
    @Bean
    public HibernatePropertiesCustomizer pajamasHibernatePropertiesCustomizer() {
        return properties -> {
            for (HibernatePropertiesEnum propertiesEnum : HibernatePropertiesEnum.values()) {
                String key = propertiesEnum.getKey();
                String defaultValue = propertiesEnum.getDefaultValue();
                properties.put(key, defaultValue);
            }
        };
    }
}
